(function() {
  var express, fs, iced, mkdirp, moment, path, pgsql, router, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  moment = require('moment');

  fs = require('fs');

  mkdirp = require('mkdirp');

  path = require('path');

  router = express.Router();

  module.exports = router;

  router.all('/', function(req, res, next) {
    var client, done, err, record, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.stat_title = '助农取款资金池变更汇总';
    res.locals.start_date = req.body.start_date || req.query.start_date || moment().format('YYYY-MM-DD');
    res.locals.end_date = req.body.end_date || req.query.end_date || moment().format('YYYY-MM-DD');
    where_clause = "(\n    f.name like $1 or f.agent like $1\n) and\n    a.ctime::date >= '" + res.locals.start_date + "' and\n    a.ctime::date <= '" + res.locals.end_date + "'";
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p2_fund_sum.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 30
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p2_fund_sum.coffee"
          });
          client.query("select a.p2_fund, ( select 0 - sum(amt) from pay_p2_fund_alter where type = 1 and p2_fund = a.p2_fund and ctime::date >= '" + res.locals.start_date + "' and ctime::date <= '" + res.locals.end_date + "' ) as type_1_amt, ( select 0 - sum(amt) from pay_p2_fund_alter where type = 2 and p2_fund = a.p2_fund and ctime::date >= '" + res.locals.start_date + "' and ctime::date <= '" + res.locals.end_date + "' ) as type_2_amt from pay_p2_fund_alter as a left join pay_p2_fund as f on f.uuid = a.p2_fund where " + where_clause + " group by a.p2_fund", ["%" + res.locals.keyword + "%"], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 51
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.records = result.rows;
          res.locals.total = res.locals.records.length;
          (function(__iced_k) {
            var _i, _len, _ref, _results, _while;
            _ref = res.locals.records;
            _len = _ref.length;
            _i = 0;
            _while = function(__iced_k) {
              var _break, _continue, _next;
              _break = __iced_k;
              _continue = function() {
                return iced.trampoline(function() {
                  ++_i;
                  return _while(__iced_k);
                });
              };
              _next = _continue;
              if (!(_i < _len)) {
                return _break();
              } else {
                record = _ref[_i];
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p2_fund_sum.coffee"
                  });
                  client.query("select a.remains, f.fund_total as fund_total, f.fund_remains as fund_remains, f.name as fund_name, f.agent as fund_agent, f.contact as fund_contact from pay_p2_fund_alter as a left join pay_p2_fund as f on f.uuid = a.p2_fund where " + where_clause + " and p2_fund = $2 order by a.serial limit 1", ["%" + res.locals.keyword + "%", record.p2_fund], __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 68
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    done(client);
                    return next(err);
                  }
                  record.remains = result.rows[0].remains;
                  record.fund_total = result.rows[0].fund_total;
                  record.fund_remains = result.rows[0].fund_remains;
                  record.fund_name = result.rows[0].fund_name;
                  record.fund_agent = result.rows[0].fund_agent;
                  return _next(record.fund_contact = result.rows[0].fund_contact);
                });
              }
            };
            _while(__iced_k);
          })(function() {
            done();
            return res.render('trstat/p2_fund_sum');
          });
        });
      };
    })(this));
  });

}).call(this);
